using System;
using System.Collections.Generic;
using System.Linq;

namespace ICSharpCode.TextEditor.Formatting.TSqlFormatter.Formatters
{
    public class TSqlStandardFormatterOptions
    {
        private static readonly TSqlStandardFormatterOptions _defaultOptions = new TSqlStandardFormatterOptions();

        private string _indentString;

        public string IndentString
        {
            get
            {
                return _indentString;
            }
            set
            {
                _indentString = value.Replace("\\t", "\t").Replace("\\s", " ");
            }
        }

        public int SpacesPerTab { get; set; }

        public int MaxLineWidth { get; set; }

        public bool ExpandCommaLists { get; set; }

        public bool TrailingCommas { get; set; }

        public bool SpaceAfterExpandedComma { get; set; }

        public bool ExpandBooleanExpressions { get; set; }

        public bool ExpandCaseStatements { get; set; }

        public bool ExpandBetweenConditions { get; set; }

        public bool UppercaseKeywords { get; set; }

        public bool BreakJoinOnSections { get; set; }

        public bool HTMLColoring { get; set; }

        public bool KeywordStandardization { get; set; }

        public bool ExpandInLists { get; set; }

        public int NewClauseLineBreaks { get; set; }

        public int NewStatementLineBreaks { get; set; }

        public TSqlStandardFormatterOptions()
        {
            IndentString = "\t";
            SpacesPerTab = 4;
            MaxLineWidth = 999;
            ExpandCommaLists = true;
            TrailingCommas = false;
            SpaceAfterExpandedComma = false;
            ExpandBooleanExpressions = true;
            ExpandBetweenConditions = true;
            ExpandCaseStatements = true;
            UppercaseKeywords = true;
            BreakJoinOnSections = false;
            HTMLColoring = false;
            KeywordStandardization = false;
            ExpandInLists = true;
            NewClauseLineBreaks = 1;
            NewStatementLineBreaks = 2;
        }

        public TSqlStandardFormatterOptions(string serializedString)
            : this()
        {
            if (string.IsNullOrEmpty(serializedString))
            {
                return;
            }
            string[] array = serializedString.Split(',');
            foreach (string text in array)
            {
                string[] array2 = text.Split('=');
                string text2 = array2[0];
                string text3 = array2[1];
                switch (text2)
                {
                    case "IndentString":
                        IndentString = text3;
                        break;
                    case "SpacesPerTab":
                        SpacesPerTab = Convert.ToInt32(text3);
                        break;
                    case "MaxLineWidth":
                        MaxLineWidth = Convert.ToInt32(text3);
                        break;
                    case "ExpandCommaLists":
                        ExpandCommaLists = Convert.ToBoolean(text3);
                        break;
                    case "TrailingCommas":
                        TrailingCommas = Convert.ToBoolean(text3);
                        break;
                    case "SpaceAfterExpandedComma":
                        SpaceAfterExpandedComma = Convert.ToBoolean(text3);
                        break;
                    case "ExpandBooleanExpressions":
                        ExpandBooleanExpressions = Convert.ToBoolean(text3);
                        break;
                    case "ExpandBetweenConditions":
                        ExpandBetweenConditions = Convert.ToBoolean(text3);
                        break;
                    case "ExpandCaseStatements":
                        ExpandCaseStatements = Convert.ToBoolean(text3);
                        break;
                    case "UppercaseKeywords":
                        UppercaseKeywords = Convert.ToBoolean(text3);
                        break;
                    case "BreakJoinOnSections":
                        BreakJoinOnSections = Convert.ToBoolean(text3);
                        break;
                    case "HTMLColoring":
                        HTMLColoring = Convert.ToBoolean(text3);
                        break;
                    case "KeywordStandardization":
                        KeywordStandardization = Convert.ToBoolean(text3);
                        break;
                    case "ExpandInLists":
                        ExpandInLists = Convert.ToBoolean(text3);
                        break;
                    case "NewClauseLineBreaks":
                        NewClauseLineBreaks = Convert.ToInt32(text3);
                        break;
                    case "NewStatementLineBreaks":
                        NewStatementLineBreaks = Convert.ToInt32(text3);
                        break;
                    default:
                        throw new ArgumentException("Unknown option: " + text2);
                }
            }
        }

        public string ToSerializedString()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            if (IndentString != _defaultOptions.IndentString)
            {
                dictionary.Add("IndentString", IndentString);
            }
            if (SpacesPerTab != _defaultOptions.SpacesPerTab)
            {
                dictionary.Add("SpacesPerTab", SpacesPerTab.ToString());
            }
            if (MaxLineWidth != _defaultOptions.MaxLineWidth)
            {
                dictionary.Add("MaxLineWidth", MaxLineWidth.ToString());
            }
            if (ExpandCommaLists != _defaultOptions.ExpandCommaLists)
            {
                dictionary.Add("ExpandCommaLists", ExpandCommaLists.ToString());
            }
            if (TrailingCommas != _defaultOptions.TrailingCommas)
            {
                dictionary.Add("TrailingCommas", TrailingCommas.ToString());
            }
            if (SpaceAfterExpandedComma != _defaultOptions.SpaceAfterExpandedComma)
            {
                dictionary.Add("SpaceAfterExpandedComma", SpaceAfterExpandedComma.ToString());
            }
            if (ExpandBooleanExpressions != _defaultOptions.ExpandBooleanExpressions)
            {
                dictionary.Add("ExpandBooleanExpressions", ExpandBooleanExpressions.ToString());
            }
            if (ExpandBetweenConditions != _defaultOptions.ExpandBetweenConditions)
            {
                dictionary.Add("ExpandBetweenConditions", ExpandBetweenConditions.ToString());
            }
            if (ExpandCaseStatements != _defaultOptions.ExpandCaseStatements)
            {
                dictionary.Add("ExpandCaseStatements", ExpandCaseStatements.ToString());
            }
            if (UppercaseKeywords != _defaultOptions.UppercaseKeywords)
            {
                dictionary.Add("UppercaseKeywords", UppercaseKeywords.ToString());
            }
            if (BreakJoinOnSections != _defaultOptions.BreakJoinOnSections)
            {
                dictionary.Add("BreakJoinOnSections", BreakJoinOnSections.ToString());
            }
            if (HTMLColoring != _defaultOptions.HTMLColoring)
            {
                dictionary.Add("HTMLColoring", HTMLColoring.ToString());
            }
            if (KeywordStandardization != _defaultOptions.KeywordStandardization)
            {
                dictionary.Add("KeywordStandardization", KeywordStandardization.ToString());
            }
            if (ExpandInLists != _defaultOptions.ExpandInLists)
            {
                dictionary.Add("ExpandInLists", ExpandInLists.ToString());
            }
            if (NewClauseLineBreaks != _defaultOptions.NewClauseLineBreaks)
            {
                dictionary.Add("NewClauseLineBreaks", NewClauseLineBreaks.ToString());
            }
            if (NewStatementLineBreaks != _defaultOptions.NewStatementLineBreaks)
            {
                dictionary.Add("NewStatementLineBreaks", NewStatementLineBreaks.ToString());
            }
            NewStatementLineBreaks = 2;
            if (dictionary.Count == 0)
            {
                return string.Empty;
            }
            return string.Join(",", dictionary.Select((KeyValuePair<string, string> kvp) => kvp.Key + "=" + kvp.Value).ToArray());
        }
    }
}
